package com.gewu.pm.ui.activity.approval;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.GoldBeanApprovalBean;
import com.gewu.pm.bean.GoodsBean;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.activity.promotion.PromotionProductActivity;
import com.gewu.pm.ui.adapter.funds.GoldBeanApprovalAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 买赠活动审批详情页面----------暂时没有区分是审核还是申请
 * 
 * type: 1=待审批，2=已通过，3=已驳回
 */
public final class ApprovalDetailActivity extends AppActivity {

    private TextView mStatusView;
    private RelativeLayout mLayoutMainProduct;
    private RelativeLayout mLayoutGiftProduct;
    private TextView mRejectRecordView;
    private RecyclerView mApprovalFlowView;
    private LinearLayout mLayoutBottomButtons;
    private TextView mBtnRejectInitiator;
    private TextView mBtnRejectPrevious;
    private TextView mBtnApprove;

    private GoldBeanApprovalAdapter mApprovalAdapter;

    private int mType;  // 1=待审批，2=已通过，3=已驳回

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, ApprovalDetailActivity.class);
        intent.putExtra(IntentKey.TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.approval_detail_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mStatusView = findViewById(R.id.tv_status);
        mLayoutMainProduct = findViewById(R.id.layout_main_product);
        mLayoutGiftProduct = findViewById(R.id.layout_gift_product);
        mRejectRecordView = findViewById(R.id.tv_reject_record);
        mApprovalFlowView = findViewById(R.id.rv_approval_flow);
        mLayoutBottomButtons = findViewById(R.id.layout_bottom_buttons);
        mBtnRejectInitiator = findViewById(R.id.btn_reject_initiator);
        mBtnRejectPrevious = findViewById(R.id.btn_reject_previous);
        mBtnApprove = findViewById(R.id.btn_approve);

        // 设置 RecyclerView
        mApprovalFlowView.setLayoutManager(new LinearLayoutManager(this));
        mApprovalAdapter = new GoldBeanApprovalAdapter(this);
        mApprovalFlowView.setAdapter(mApprovalAdapter);

        // 设置点击事件
        setOnClickListener(mLayoutMainProduct, mLayoutGiftProduct, mRejectRecordView,
                mBtnRejectInitiator, mBtnRejectPrevious, mBtnApprove);
    }

    @Override
    protected void initData() {
        mType = getInt(IntentKey.TYPE, 1);
        
        // 根据类型设置UI
        updateUI();
        
        // 加载审批流程数据
        loadApprovalFlow();
    }

    /**
     * 根据类型更新UI
     */
    private void updateUI() {
        switch (mType) {
            case 1: // 待审批
                setTitle("买赠活动-待审批");
                mStatusView.setText("待审批");
                mStatusView.setBackgroundResource(R.drawable.shape_red_2_kong);
                mStatusView.setTextColor(getResources().getColor(R.color.red_app));
                mLayoutBottomButtons.setVisibility(View.VISIBLE);
                break;
            case 2: // 已通过
                setTitle("买赠活动-已通过");
                mStatusView.setText("已通过");
                mStatusView.setBackgroundResource(R.drawable.shape_green_2_kong);
                mStatusView.setTextColor(getResources().getColor(R.color.green_label));
                mLayoutBottomButtons.setVisibility(View.GONE);
                break;
            case 3: // 已驳回
                setTitle("买赠活动-已驳回");
                mStatusView.setText("已驳回");
                mStatusView.setBackgroundResource(R.drawable.shape_grey_2_kong);
                mStatusView.setTextColor(getResources().getColor(R.color.cb9));
                mLayoutBottomButtons.setVisibility(View.GONE);
                // 已驳回时显示驳回记录按钮
                mRejectRecordView.setVisibility(View.VISIBLE);
                break;
        }
    }

    /**
     * 加载审批流程数据（根据type创建不同状态）
     */
    private void loadApprovalFlow() {
        List<GoldBeanApprovalBean> approvalList = new ArrayList<>();

        switch (mType) {
            case 1: // 待审批
                // 发起人（已提交）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "翟辉·已提交", 1));
                // 审批人（已通过）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "陈浩·审核通过", 1));
                // 审批人（已通过）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "熊文亮·审核通过", 1));
                // 审批人（待审批）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "胡凌·待审批", 0));
                // 抄送人（未到达）
                approvalList.add(new GoldBeanApprovalBean("", "尹法德", 0));
                approvalList.add(new GoldBeanApprovalBean("", "邹彦", 0));
                break;
                
            case 2: // 已通过
                // 发起人（已提交）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "翟辉·已提交", 1));
                // 审批人（已通过）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "陈浩·审核通过", 1));
                // 审批人（已通过）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "熊文亮·审核通过", 1));
                // 审批人（已通过）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "胡凌·审核通过", 1));
                // 抄送人
                approvalList.add(new GoldBeanApprovalBean("2025/6/18 16:00", "尹法德", 1));
                approvalList.add(new GoldBeanApprovalBean("2025/6/18 16:00", "邹彦", 1));
                break;
                
            case 3: // 已驳回
                // 发起人（已提交）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "翟辉·已提交", 1));
                // 审批人（已通过）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "陈浩·审核通过", 1));
                // 审批人（已通过）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "熊文亮·审核通过", 1));
                // 审批人（已驳回）
                approvalList.add(new GoldBeanApprovalBean("2025/8/18 16:00", "胡凌·驳回发起人", 2));
                // 抄送人（未到达）
                approvalList.add(new GoldBeanApprovalBean("", "尹法德", 0));
                approvalList.add(new GoldBeanApprovalBean("", "邹彦", 0));
                break;
        }

        mApprovalAdapter.setData(approvalList);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.layout_main_product) {
            // 点击促销主品 - 跳转到促销主品页面
            List<GoodsBean> goodsList = createTestGoodsList();
            PromotionProductActivity.start(this, 1, goodsList);
        } else if (id == R.id.layout_gift_product) {
            // 点击促销赠品 - 跳转到促销赠品页面
            List<GoodsBean> goodsList = createTestGoodsList();
            PromotionProductActivity.start(this, 2, goodsList);
        } else if (id == R.id.tv_reject_record) {// 驳回记录
            RejectionRecordActivity.start(getActivity());

        } else if (id == R.id.btn_reject_initiator) {
            // 驳回发起人
            toast("驳回发起人");
        } else if (id == R.id.btn_reject_previous) {
            // 驳回上一级
            toast("驳回上一级");
        } else if (id == R.id.btn_approve) {
            // 审核通过
            toast("审核通过");
        }
    }

    /**
     * 创建测试商品数据
     */
    private List<GoodsBean> createTestGoodsList() {
        List<GoodsBean> list = new ArrayList<>();
        
        GoodsBean goods1 = new GoodsBean();
        goods1.setName("祖名老豆腐");
        goods1.setCode("SP001");
        goods1.setQuantity(3);
        goods1.setPrice("25.00");
        
        list.add(goods1);
        
        return list;
    }
}

